<style>
	ul {padding: 0px; margin:0px; display: inline;}
	ul li {line-height: 1.2em; font-size: 1.2em}
	.optionsbox{width: 100%; height: auto; border: 1px solid #8FBCE9; padding: 5px; margin-bottom: 10px;}
	.optionsboxbottom{margin-top: 10px; text-align: right; padding: 5px}
	.resultsbox{width: 100%; height: auto; padding: 5px; overflow: auto; border: 1px solid #8FBCE9 }
	.hide{display: none}
	.ext-el-mask{z-index:20000;position:fixed;top:0;left:0;-moz-opacity:0.5;opacity:.50;filter:alpha(opacity=50);background-color:#CCC;width:100%;height:100%;zoom:1;}
	.ext-el-mask-msg{z-index:20001;position:absolute;top:0;left:0;border:1px solid #6593cf;background:#c3daf9;padding:2px;}
	.ext-el-mask-msg div{padding:5px 10px 5px 10px;background:#eee;border:1px solid #a3bad9;color:#333;font:normal 12px tahoma,arial,helvetica,sans-serif;cursor:wait;}
	.x-mask-loading div{padding:5px 10px 5px 25px;background:#eee url(images/loading.gif) no-repeat 5px 5px;line-height:16px;}
	#loading-msg{position:fixed;top:45%!important;left:43%!important;z-index:20001!important;}
</style>
<div id="loading-mask" class="ext-el-mask" style="display: none;"></div>
<div id="loading-msg" class="ext-el-mask-msg x-mask-loading" style="display: none;">
  <div>Processing...</div>
</div>
<?php
/* Configuration */
$path = "http://".$_SERVER['HTTP_HOST']."/whmcs/modules/admin/seller_manager/ajax.php";
$sale_dept_id  = "'2', '11', '14', '4', '6'";

/******************************************************************************************/
/*Create saler tracking table
$dbtable = "CREATE TABLE IF NOT EXISTS mod_salertracking (
				id INT AUTO_INCREMENT PRIMARY KEY,
				userid INT NOT NULL,
				clientid INT NOT NULL,
				comment TEXT CHARACTER SET utf8 COLLATE utf8_general_ci
			)";
mysql_query($dbtable);			
/******************************************************************************************/

function gen_seller_list($selectid)
{
	global $sellers;
	$tmp = "<select name='".$selectid."' id='".$selectid."'>
				<option value=''>Nobody</options>";
	foreach ($sellers as $s)
	{
		$tmp .= "<option value=".$s['id'].">".$s['fullname']."</option>";
	}
	$tmp .= "</select>";
	return $tmp;
}

/******************************************************************************************/
/* get seller list */
$sellers = array();
$rs = mysql_query("SELECT id, roleid, username, concat(lastname, ' ', firstname) as fullname FROM tbladmins WHERE roleid IN (".$sale_dept_id.")");
while($row = mysql_fetch_assoc($rs))
{
	$sellers[]= $row;
}

/* get client list*/
$clients = array();
$rs = mysql_query("SELECT id, concat(lastname, ' ', firstname) as fullname FROM tblclients WHERE status='Active';");
while($row = mysql_fetch_array($rs))
{
	$clients[] = $row;
}
/******************************************************************************************/	
?>
<div id="msg" class="infobox hide"></div>

<div class="optionsbox">
	<div id="addtracking">
		<form id="frmaddtracking" method="post">
			<div class="tablebg">
			<table width="100%" class="datatable" cellpadding="4">
				<tr><th colspan="2">Add seller to a client</th></tr>
				<tr>
					<td width="80" style="text-align:right">Seller: </td>
					<td style="text-align:left"><?php echo gen_seller_list('addcbosaler'); ?></td>
				</tr>
				<tr>
					<td width="80" style="text-align:right">Client: </td>
					<td style="text-align:left"><?php 
					echo "<select name='cboclient' id='cboclient'><option value=''>Nobody</options>";
					foreach ($clients as $c)
					{
						echo "<option value=".$c['id'].">#".$c['id']." - ".$c['fullname']."</option>";
					}
					echo "</select>";
					 ?></td>
				</tr>
				<tr>
					<td width="80" style="text-align:right">Comment: </td>
					<td style="text-align:left"><input name="comment" size="50"></input></td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="submit" id="addtracking" name="addtracking" value="Add" />
					</td>
				</tr>
			</table>
			</div>
		</form>
	</div>
	<br/>
	<div id="viewtracking">
		<form id="frmviewtracking" method="post">
			<div class="tablebg">
			<table width="100%" class="datatable" cellpadding="4">
				<tr><th colspan="2">View client list</th></tr>
				<tr>
					<td width="80" style="text-align:right">Seller: </td>
					<td style="text-align:left"><?php echo gen_seller_list('viewcbosaler'); ?></td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="submit" name="viewtracking" value="View" />
					</td>
				</tr>
			</table>
			</div>
		</form>
	</div>
		
</div>
<br/><br/>
<div class="resultsbox hide">
	<div class="resultitems"></div>
</div>
<script language="javascript" type="text/javascript">
	$().ready(function(){
		$("#addtracking").submit(function(){
			if($("#addcbosaler").val() != "" && $("#cboclient").val() != "")
			{
				$("#loading-mask").css('display','');
				$("#loading-msg").css('display','');
				$.ajax({
					url: '<?php echo $path; ?>',
					type: "post",
					data: "type=addtracking&"+$("#frmaddtracking").serialize(),
					success: function(data){		
						$("#loading-mask").css('display','none');
						$("#loading-msg").css('display','none');			
						$("#msg").text(data);
						$("#msg").removeClass("hide");
						setTimeout(function(){$("#msg").addClass("hide")}, 8000);
					},
					error: function(){
						alert('fail');
					}				
				});
			}
			return false;
		});
		
		$("#viewtracking").submit(function(){			
			if($("#viewcbosaler").val() != "")
			{
				$("#loading-mask").css('display','');
				$("#loading-msg").css('display','');
				$.ajax({
					url: '<?php echo $path; ?>',
					type: "post",
					data: "type=viewtracking&"+$("#frmviewtracking").serialize(),
					success: function(data){
						$("#loading-mask").css('display','none');
						$("#loading-msg").css('display','none');					
						$(".resultitems").html(data);
						$(".resultsbox").removeClass('hide');
					},
					error: function(){
						alert('fail');
					}				
				});
			}
			return false;
		});
	});
</script>